# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import json
# useful for handling different item types with a single interface
from scrapy.utils.project import get_project_settings
import os

# 视频信息管道
class DetailsPipeline:
    def open_spider(self, spider):
        # 获取项目根目录
        settings = get_project_settings()
        project_root = settings.get('PROJECT_ROOT',
                                    os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))

        # 创建数据目录（绝对路径）
        self.data_dir = os.path.join(project_root, 'data_json')
        os.makedirs(self.data_dir, exist_ok=True)

    def process_item(self, item, spider):
        data_dict = {
            'page_number': item['page_number'],
            'title': item['title'],
            'episode': item['episode'],
            'episode_urls': item['episode_urls'],
        }
        # 构建绝对文件路径
        file_path = os.path.join(self.data_dir, f"{item['page_number']}")
        os.makedirs(file_path, exist_ok=True)
        file_path = os.path.join(file_path, f"{item['title']}.json")


        with open(file_path, 'w', encoding='utf-8') as f:
            json.dump(data_dict, f, ensure_ascii=False, indent=4)

    def close_spider(self, spider):
        pass

# ts下载管道
class VideoScrapyPipeline:
    def process_item(self, item, spider):
        # 获取项目根目录的绝对路径
        base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        # C:\PythonProject\video_scrapy\video_scrapy\video
        video_di = os.path.join(base_dir, 'video')
        # C:\PythonProject\video_scrapy\video_scrapy\video\title
        video_dir = os.path.join(video_di, item['title'])
        # C:\PythonProject\video_scrapy\video_scrapy\video\视频名称\剧集数
        video_dir_juji = os.path.join(video_dir, item['last_number'])

        # 确保目录存在
        os.makedirs(video_di, exist_ok=True)
        os.makedirs(video_dir, exist_ok=True)
        os.makedirs(video_dir_juji, exist_ok=True)

        # 构建完整文件路径
        file_path = os.path.join(video_dir_juji, item["file_name"])

        # 写入文件
        with open(file_path, 'wb') as f:
            f.write(item['video_con'])

        return item

